package com.demo.springmvc.controller;

import com.demo.springmvc.Dao.DepartmentDao;
import com.demo.springmvc.Dao.EmployeeDao;
import com.demo.springmvc.entities.Department;
import com.demo.springmvc.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @author liangliang
 */
@Controller
@RequestMapping("/restCrud")
public class RestCrudController {

    private final EmployeeDao employeeDao;
    private final DepartmentDao departmentDao;

    @Autowired
    public RestCrudController(EmployeeDao employeeDao, DepartmentDao departmentDao) {
        this.employeeDao = employeeDao;
        this.departmentDao = departmentDao;
    }

    @ModelAttribute
    public void getEmp(@RequestParam(value="id",required=false) Integer id, Model model){
        if(id != null){
            model.addAttribute("emp", employeeDao.getEmployee(id));
        }
    }

    // 查
    @RequestMapping("/list")
    public String list(Model model) {
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("employees", employees);
        return "list";
    }

    @RequestMapping(value = "emp/detail/{id}", method = RequestMethod.GET)
    public String getEmployee(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.getEmployee(id);
        model.addAttribute("emp", employee);
        return "empDetail";
    }

    // 更新员工信息页面
    @RequestMapping(value = "emp/{id}", method = RequestMethod.GET)
    public String updateEmp(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.getEmployee(id);
        model.addAttribute("emp",employee);
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "updateEmp";
    }

    // 更新员工信息 利用ModelAttribute的特性
    @RequestMapping(value = "emp", method = RequestMethod.PUT)
    public String updateEmployee(@ModelAttribute("emp") Employee employee) {
        employeeDao.save(employee, employee.getDepartment().getId());
        return "redirect:/restCrud/list";
    }

    // 添加新员工 /
    @RequestMapping(value = "emp", method = RequestMethod.GET)
    public String addEmployee(Model model) {
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "addEmp";
    }

    // 添加
    @RequestMapping(value = "emp", method = RequestMethod.POST)
    public String addEmployee(Employee employee) {
        if (employee == null || employee.getDepartment() == null) {
            return "error";
        }
        employeeDao.save(employee, employee.getDepartment().getId());
        return "redirect:/restCrud/list";
    }

    // 删除
    @RequestMapping(value = "emp/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id) {
        employeeDao.remove(id);
        return "redirect:/restCrud/list";
    }
}
